//
//  MainTabBarController.m
//  FindPreciousFrameWork
//
//  Created by TianZhen on 16/4/3.
//  Copyright © 2016年 MoreMoney. All rights reserved.
//

#import "FrameWorkTabBarController.h"
#import "ViewController.h"

@interface FrameWorkTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic,assign) BOOL isLogin;

@end

@implementation FrameWorkTabBarController

#pragma mark - ======== viewDidLoad ========
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //指定代理
    self.delegate = self;
  // ***********  创建并添加子控制器  ***********  //
    
    // 主页
    [self addStoryboardNamed:@"Main" title:@"主页" imageName:@"home"];
    
    // 发现
    [self addStoryboardNamed:@"Discover" title:@"发现" imageName:@"discovery"];
    
    // 社区
    [self addStoryboardNamed:@"Community" title:@"社区" imageName:@"community"];
    
    // 动态
    [self addStoryboardNamed:@"Dynamic" title:@"动态" imageName:@"status"];
    
    // 我的
    
    ViewController *CZmainController = [[ViewController alloc]init];

    [self addController:CZmainController andTitle:@"我的"];
    

    
    
    
  // ***********  创建并添加子控制器  ***********  //
}





#pragma mark - ======== 添加控制器方法 ========
- (void)addStoryboardNamed:(NSString *)sbName title:(NSString *)title imageName:(NSString *)imageName
{
    // 获取指定sb
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:sbName bundle:nil];
    
    // 拿到sb指向的控制器
    UIViewController *viewController = storyBoard.instantiateInitialViewController;
    // 设置标题
    viewController.tabBarItem.title = title;
    
    // 设置字体样式
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [viewController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    
    // 设置默认、选中图片
    NSString *norImgName = [NSString stringWithFormat:@"tab_%@_select_nor",imageName];
    NSString *selImgName = [NSString stringWithFormat:@"tab_%@_selected_nor",imageName];
    viewController.tabBarItem.image = [UIImage imageNamed:norImgName];
    viewController.tabBarItem.selectedImage = [UIImage imageNamed:selImgName];
    
    // 图片insets
    viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);

    // 添加到框架
    [self addChildViewController:viewController];
}

- (void)addController:(UIViewController *)controller andTitle:(NSString *)title {
    
    // 设置字体样式
    controller.title = title;

    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:controller];
    
    
    navController.tabBarItem.title = title;
    
    // 设置字体样式
    [navController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [navController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    
    // 设置默认、选中图片
    NSString *norImgName = [NSString stringWithFormat:@"tab_user_select_nor"];
    NSString *selImgName = [NSString stringWithFormat:@"tab_user_selected_nor"];
    navController.tabBarItem.image = [UIImage imageNamed:norImgName];
    navController.tabBarItem.selectedImage = [UIImage imageNamed:selImgName];
    
    // 图片insets
    navController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
    
    // 添加到框架
    
    
    [self addChildViewController:navController];
    
 
    
}

#pragma mark tabbar是否跳转到登录界面  代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.isLogin = [XBSaveDataTool boolForKey:loginStause];
    
    if ([viewController.tabBarItem.title isEqualToString:@"动态"]||[viewController.tabBarItem.title isEqualToString:@"我的"])
    {
        if(self.isLogin)
            return YES;
        else
        {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
            UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"navLogin"];
            [self presentViewController:vc animated:YES completion:nil];
            
            return NO;
        }
    }
    
    return YES;
}





#pragma mark - ======== 控制器跳转 ========
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

#pragma mark - ======== 内存警告 ========
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
